import numpy as np
import cv2

def D3(src, table):
    """
    三围颜色找查表
    :param src:
    :param table:
    :return:
    """
    rows, cols, channel = src.shape[0], src.shape[1], src.shape[2]
    if channel != 3:
        raise ValueError('the channel of src must be 3')
    if table.shape[0] != 256 or table.shape[1] != 256 or table.shape[1] != 256:
        raise ValueError('the table  must be 256x256x256')
    mat = np.empty([rows, cols], np.uint8)
    for r in range(rows):
        for c in range(cols):
            tmp = src[r, c]
            mat[r, c] = table[tmp[0], tmp[1], tmp[2]]
    return mat


def D3_Splitter(src, table):
    """
    三围颜色找查表
    :param src:
    :param table:
    :return:
    """
    rows, cols, channel = src.shape[0], src.shape[1], src.shape[2]
    mat = np.empty([rows, cols], np.float16)
    for r in range(rows):
        for c in range(cols):
            mat[r, c] = 0
            for channel in range(0, 3):
                mat[r, c] = mat[r, c] + table[channel, src[r, c, channel]]

    return np.uint8(mat)